package com.dsr.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.dsr.bean.FlowerBean;
import com.dsr.consts.PageResult;
import com.dsr.consts.Result;

import com.dsr.dao.FlowerDao;
import com.dsr.dao.SpitDao;
import com.dsr.entity.Flower;
import com.dsr.service.FlowerService;
import com.dsr.util.IdWorker;
import com.dsr.util.RedisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * (Flower)表服务实现类
 *
 * @author dsr
 * @since 2020-08-13
 */
@Service("flowerService")
@Transactional
public class FlowerServiceImpl implements FlowerService {
    @Autowired
    private FlowerDao flowerDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private SpitDao spitDao;

    @Override
    public Result getEntity(String id) {
        if(RedisUtil.getValue(id)!=null){
            FlowerBean  value = (FlowerBean) RedisUtil.getValue(id);
            assert value != null;
            value.setCount(spitDao.countSpitByFlowerId(id));
            return Result.success(value);
        }
        Flower entity = flowerDao.selectByPrimaryKey(id);
        FlowerBean flowerBean = new FlowerBean();
        BeanUtil.copyProperties(entity,flowerBean);
        flowerBean.setImgUrlList(Arrays.asList(entity.getImgUrl().split(",")));
        flowerBean.setCount(spitDao.countSpitByFlowerId(id));
        RedisUtil.setValue(id,flowerBean);
        return Result.success(flowerBean);
    }
    @Override
    public Result save(Flower flower) {
        flower.setId(String.valueOf(idWorker.nextId()));
        flower.setCreateDate(DateTime.now());
        flower.setUpdateDate(DateTime.now());
        flower.setCollectNumber(0L);
        flower.setSales(0L);
        flower.setDel(0);
        flowerDao.insertSelective(flower);
        return Result.success("保存成功");
    }

    @Override
    public Result update(Flower flower) {
        flower.setUpdateDate(DateTime.now());
        flowerDao.updateByPrimaryKeySelective(flower);
        RedisUtil.delete(flower.getId());
        return Result.success("修改成功");

    }

    @Override
    public Result delete(String id) {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        if (StringUtils.isNotEmpty(id)) {
            RedisUtil.delete(id);
            Flower flower = new Flower();
            flower.setDel(1);
            List<String> ids = Arrays.asList(id.split(","));
            Example example = new Example(Flower.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andIn("id", ids);
            flowerDao.updateByExampleSelective(flower,example);
            return Result.success("删除成功");
        }
        return Result.error("请选择要删除的行");
    }

    @Override
    public Result query(int page, int size, Flower flower) {
        Example example = new Example(Flower.class);
        example.orderBy("updateDate").desc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("del", 0);
        if(flower!=null){
            if(StringUtils.isNotEmpty(flower.getTitle()))
                criteria.andLike("title","%"+flower.getTitle()+"%");
            if (flower.getQueryDate()!=null){
                criteria.andBetween("createDate",flower.getCreateDate(),flower.getQueryDate());
            }
        }
        PageHelper.startPage(page, size);
        List<Flower> entityList = flowerDao.selectByExample(example);
        PageInfo<Flower> pageInfo = new PageInfo<>(entityList);
        return Result.success(new PageResult<FlowerBean>(pageInfo.getTotal(), flowerBeanList(pageInfo.getList())));
    }
    @Override
    public Result list() {
        Example example = new Example(Flower.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("del", 0);
        return Result.success(flowerDao.selectByExample(example));
    }
    private List<FlowerBean> flowerBeanList(List<Flower> flowerList){
        List<FlowerBean> flowerBeanList = new ArrayList<>();
        flowerList.forEach(item->{
            FlowerBean flowerBean = new FlowerBean();
            BeanUtils.copyProperties(item,flowerBean);
            if (item.getImgUrl().contains(",")) {
                String[] split = item.getImgUrl().split(",");
                flowerBean.setImgUrl(split[0]);
            }else {
                flowerBean.setImgUrl(item.getImgUrl());
            }
            flowerBeanList.add(flowerBean);
        });
        return flowerBeanList;
    }
}